package com.doucome.snapshot.view
{
	import com.doucome.snapshot.User;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.ActivityEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.system.Security;
	import flash.system.SecurityPanel;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import com.doucome.snapshot.Local;
	import flash.display.Stage;
	
	/**
	 * ...
	 * @author wkl
	 */
	public class MCamera extends MovieClip
	{
		private var camera:Camera;
		private var _video_view:MovieClip;
		private const v_w:int = 480;
		private const v_h:int = 360;
		private var _video:Video;
		private var _photoBmd:BitmapData; //ccd
		private var msg_txt:TextField;
		private var _count_down:CountDown;
		private var _count_down_effect:CountDownEffect;
		private var _ts_ks:TsKs;
		private var _ts_camera_no:MovieClip;
		private var _ts_camera_cancel:MovieClip;
		private var _ts_nopaper:MovieClip;
		private var _btn_confirm:SimpleButton;
		private var _btn_upload:SimpleButton;
		private var _paper:int = 0; //相纸
		
		public static const TAKE_PIC_EVENT:String = "CameraTakePicEvent";
		public static const PAPER_EMPTY_EVENT:String = "CameraNoXzEvent";
		public static const CAMERA_FAIL_EVENT:String = "CameraFailEvent";
		
		public function MCamera()
		{
			
			init();
		}
		
		private function init():void
		{
			
			_video_view = getChildByName("camera_view") as MovieClip;
			_ts_camera_no = getChildByName("camera_no") as MovieClip;
			_ts_camera_cancel = getChildByName("camera_cancel") as MovieClip;
			_btn_confirm = getChildByName("btn_confirm") as SimpleButton;
			_btn_upload = getChildByName("btn_upload") as SimpleButton;
			_ts_nopaper = getChildByName("no_paper") as MovieClip;
			
			_ts_nopaper.stop();
			//ccd
			_photoBmd = new BitmapData(v_w, v_h, true);
			camera = Camera.getCamera();
			//没有摄像头的情况
			if (camera == null)
			{
				_ts_camera_no.visible = true;
				_btn_confirm.visible = true;
				_btn_confirm.addEventListener(MouseEvent.CLICK, goUpload);
				
			}
			else
			{
				camera.setQuality(0, 100);
				camera.setMode(v_w, v_h, 30, false);
				camera.addEventListener(StatusEvent.STATUS, staHandler);
				//camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
				initCamera();
			}
		
		}
		
		private function initCamera():void
		{
			
			_video = new Video();
			_video.width = v_w;
			_video.height = v_h;
			_video.attachCamera(camera);
			_video_view.addChild(_video);
			
			//count_down
			
			_count_down = new CountDown();
			_count_down.x = _video_view.x + (_video_view.width / 2);
			_count_down.y = _video_view.y + (_video_view.height / 2);
			_count_down.addEventListener(CountDown.COUNT_DOWN_END_EVENT, onCountDownEnd);
			_count_down.visible = false;
			_count_down.stop();
			_video_view.addChild(_count_down);
			
			//_ts_ks 按空格键开始提示
			_ts_ks = new TsKs();
			_ts_ks.x = _video_view.x + (_video_view.width / 2 - _ts_ks.width / 2);
			_ts_ks.y = _video_view.y + (_video_view.height - 80);
			_ts_ks.buttonMode = true;
			_ts_ks.visible = false;
			addChild(_ts_ks);
			_ts_ks.addEventListener(MouseEvent.CLICK, doCountDown);
			
		
		}
		
		
		
		//摄像头权限申请
		private function staHandler(e:StatusEvent):void
		{
			//用户拒绝开启摄像头
			if (camera.muted)
			{
				_ts_camera_cancel.visible = true;
				
				_btn_confirm.visible = true;
				_btn_confirm.addEventListener(MouseEvent.CLICK, goUpload);
			}
			else
			{
				
					reset();
			}
		
		}
		
		//按空格键开始拍摄
		private function onkeyBoardSpace(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.SPACE)
			{
				_ts_ks.visible = false;
				stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.onkeyBoardSpace);
				doCountDown(e);
			}
		}
		
		
		
		//倒计时开始
		private function doCountDown(e:Event):void
		{
			
			_ts_ks.visible = false;
			_count_down.visible = true;
			_count_down.gotoAndPlay(1);
		
		}
		
		//倒计时结束，快门开启
		private function onCountDownEnd(e:Event):void
		{
			_count_down.visible = false;
			_count_down.stop();
			//释放快门拍照
			takePic();
			this.dispatchEvent(new Event(MCamera.TAKE_PIC_EVENT));
		}
		
		//摄像头重新授权
		private function goUpload(e:MouseEvent):void
		{
			_ts_camera_cancel.visible = false;
			_ts_camera_no.visible = false;
			_btn_confirm.visible = false;
			dispatchEvent(new Event(CAMERA_FAIL_EVENT));
			//Security.showSettings(SecurityPanel.PRIVACY);
		}
		
		//成像
		private function takePic():void
		{
			
			var bdm:BitmapData = new BitmapData(v_w, v_h, true);
			bdm.draw(_video_view);
			_photoBmd.dispose();
			_photoBmd = bdm;
		
		}
		
		//相机复位
		public function reset():void
		{
			//
			//trace("reset");
			//检查相纸情况
			if (paper <= 0)
			{
				_ts_ks.visible = false;
				_ts_nopaper.play();
				_ts_nopaper.visible = true;
				
			}
			else
			{
				_ts_nopaper.stop();
				_ts_nopaper.visible = false;
				_ts_ks.visible = true;
				stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeyBoardSpace);
				
			}
		
		}
		
		//相机待机
		public function sleep():void
		{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, onkeyBoardSpace);
			//this.visible = false;
		}
		
		public function get photoBmd():BitmapData
		{
			return _photoBmd;
		}
		
		public function set photoBmd(value:BitmapData):void
		{
			_photoBmd = value;
		}
		
		public function get paper():int
		{
			return _paper;
		}
		
		public function set paper(value:int):void
		{
			_paper = value;
		}
	
	}

}